////////////////////////////////////////////////////////////////////////////////
//(c) Intelliquip, Inc. 2001
////////////////////////////////////////////////////////////////////////////////
//$Log:
// 4    Version 10.0 (Current View)1.3         2/8/2008 4:07:27 PM    Jeff
//      Heigl      IDD#6580: cleanup to prepare for the bug fix for the
//      preferences submit redirecting to system number 1
// 3    Version 10.0 (Current View)1.2         12/3/2007 10:38:04 AM  Hai Hoang
//            IDD#6362: GUI navigations
// 2    Version 10.0 (Current View)1.1         10/24/2007 4:30:01 PM  Jeff
//      Heigl      IDD#5830: UI structuring to support the head loss popup
//      windows
// 1    Version 10.0 (Current View)1.0         10/24/2007 9:11:57 AM  Jeff
//      Heigl      
//$
//$NoKeywords$
////////////////////////////////////////////////////////////////////////////////
package com.intelliquip.hydraulicselection.control.web;

import org.w3c.dom.Element;

import com.intelliquip.core.xml.XMLDocument;
import com.intelliquip.util.ServletUtils;

public class PopupRequest {
	
	/**
	 * from {@link ScreenNames}
	 */
	private String servletUrl; 
	
	public PopupRequest() {
	}
	
	/**
	 * Write the xml and clear the state of the current popup request
	 * @param doc
	 * @param urlBuilder
	 * @return
	 */
	public Element writeXMLAndClearRequest(XMLDocument doc, UrlBuilder urlBuilder) {
		Element e = doc.createElement("popupRequest");
		if(isUrlSet()) {
			e.appendChild(doc.createCDATANode(urlBuilder.buildUrl(servletUrl, true)));
			
			String baseUrl = ServletUtils.removeParamsFromUrl(servletUrl);
			e.setAttribute("servletUrl", baseUrl);
			
			clear();
		}
		
		return e;
	}
	
	/**
	 * clear the state of the current popup request
	 */
	private void clear() {
		setServletUrl(null);
	}
	
	/**
	 * check state of the url
	 * @return
	 */
	public boolean isUrlSet() {
		return servletUrl != null && servletUrl.length() > 0;
	}

	/**
	 * Set the {@link #servletUrl} when url is valid
	 * @param servletUrl - servlet url for the popup window request (i.e "/headloss")
	 * @return - true only when the param specified is a valid url
	 */
	public boolean processPopupRequest(String servletUrl) {
		if(ScreenNames.isValidUrl(servletUrl)) {
			setServletUrl(servletUrl);
			return true;
		} else {
			return false;
		}
	}

	private void setServletUrl(String popupUrl) {
		this.servletUrl = popupUrl;
	}

}
